<?php

namespace App\Http\Middleware;

use App\Http\Responsible;
use App\Services\InjectServices;
use Closure;
use function foo\func;

class AuthMiddleware
{
    use InjectServices, Responsible;
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $token = $request->header('X_USER_TOKEN');
        $user = $this->authService->getUserByToken($token);
        if(!$user) {
            return $this->failed('You are not logged in, please login first!', 40101);
        }

        $request->setUserResolver(function () use($user){
            return $user;
        });

        $request->merge(["user" => $user]);
        return $next($request);
    }
}
